package kz.home.gui;

import kz.home.db.DBUtils;
import kz.home.utils.Utils;

import javax.microedition.lcdui.*;
import java.util.Hashtable;
import java.util.Vector;

/**
 * <strong>title</strong>
 * <p> describe
 *
 * @author: Danik <br>
 * Date: 29.07.2009  <br>
 * Time: 18:29:11  <br>
 */
public abstract class BaseForm extends List implements CommandListener{
    protected Hashtable request;
    protected BaseForm mainScreen;
    protected Display display;
    protected Command cmd_back;
    protected String req_url = "http://kino.one.kz/kino.php?ajax=true";
    protected Hashtable map = new Hashtable();


    public BaseForm(String title, BaseForm mainScreen, Display display, Hashtable request) {
        super(title, List.IMPLICIT);
        this.mainScreen = mainScreen;
        this.display = display;
        this.request = request;

        initCommand();
        setCommandListener(this);
    }

    protected void initCommand() {
        if (mainScreen != null) {
            cmd_back = new Command("Назад", Command.BACK, 1);
            addCommand(cmd_back);
        }
    }

    public void commandAction(Command command, Displayable displayable) {
        if (mainScreen != null) {
            if (command == cmd_back) {
                display.setCurrent(mainScreen);
                mainScreen.backCommandPerfomed();
            }
        }
    }

    protected void initMap(String dbName) {
        Vector allData = DBUtils.getFromDB(dbName);
        for (int i = 0; i < allData.size(); i++) {
            if (allData.elementAt(i) instanceof Vector) {
                Vector innerVector = (Vector) allData.elementAt(i);
                addToMap(map, innerVector);
            }
        }
    }

    protected void addToMap(Hashtable map, Vector dataRow){

    }

    public int append(String str) {
        return append(str, null);
    }

    protected void setValue(String value) {
        if (getParam() != null)   request.put(getParam(), value);

        Utils.print(getParam() + "[]=" + value);
    }

    protected  String getParam() {
        return null;
    }

    protected void backCommandPerfomed() {
        if (getParam() != null) request.remove(getParam());
    }
}
